//
//  LXAuthorDetailCell.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/23.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXAuthorDetailCell.h"

@implementation LXAuthorDetailCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubViews];
        self.isCleck = YES;
    }
    return self;
}
-(void)createSubViews{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction)];
    self.UserImage =  [[UIImageView alloc]init];
    self.UserImage.layer.cornerRadius = 30;
    self.UserImage.layer.masksToBounds = YES;
    self.UserImage.userInteractionEnabled = YES;
    [self.UserImage addGestureRecognizer:tap];
    self.UserImage.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.UserImage] ;
    [self.UserImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.width.height.mas_equalTo(60);
    }];
    
    self.PlayOrNot = [[UIImageView alloc]init];
    self.PlayOrNot.layer.cornerRadius = 10;
    self.PlayOrNot.userInteractionEnabled = YES;
    self.PlayOrNot.image = [UIImage imageNamed:@"cell_sound_play_n"];
    self.PlayOrNot.layer.masksToBounds = YES;
    self.PlayOrNot.backgroundColor = [UIColor grayColor];
    [self.UserImage addSubview:self.PlayOrNot];
    [self.PlayOrNot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(20);
        make.bottom.right.mas_equalTo(-20);
    }];
    
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.numberOfLines = 0;
    self.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_UserImage.mas_right).offset(SPACE);
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-SPACE+(-80)+(-SPACE));
        make.height.mas_equalTo(20);
    }];
    
    self.authorLabel = [[UILabel alloc]init];
    self.authorLabel.font = [UIFont systemFontOfSize:12];
    self.authorLabel.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:_authorLabel];
    [self.authorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLabel.mas_left);
        make.top.mas_equalTo(_titleLabel.mas_bottom).offset(SMALLSPICE);
        make.right.mas_equalTo(-10);
        make.height.mas_equalTo(20);
    }];
    
    UIImageView *play = [[UIImageView alloc]init];
    play.image = [UIImage imageNamed:@"sound_playtimes@2x.png"];
    [self.contentView addSubview:play];
    [play mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_authorLabel.mas_left);
        make.top.mas_equalTo(_authorLabel.mas_bottom).offset(9);
        make.width.mas_equalTo(12);
        make.height.mas_equalTo(12);
    }];
    
    self.playLabel = [[UILabel alloc]init];
    self.playLabel.textColor = [UIColor lightGrayColor];
    self.playLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.playLabel];
    [self.playLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(play.mas_right).offset(SMALLSPICE);
        make.top.equalTo(_authorLabel.mas_bottom).offset(SMALLSPICE);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(30);
    }];
    
    UIImageView *like = [[UIImageView alloc]init];
    like.image = [UIImage imageNamed:@"sound_likes_n@2x.png"];
    [self.contentView addSubview:like];
    [like mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_playLabel.mas_right).offset(SPACE);
        make.top.equalTo(play.mas_top);
        make.height.mas_equalTo(12);
        make.width.mas_equalTo(12);
    }];
    self.likeLabel = [[UILabel alloc]init];
    self.likeLabel.textColor = [UIColor lightGrayColor];
    self.likeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.likeLabel];
    [self.likeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(like.mas_right).offset(SMALLSPICE);
        make.top.equalTo(_playLabel.mas_top);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(30);
    }];
    
    UIImageView *com = [[UIImageView alloc]init];
    com.image = [UIImage imageNamed:@"sound_comments@2x.png"];
    [self.contentView addSubview:com];
    [com mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_likeLabel.mas_right).offset(SPACE);
        make.top.equalTo(play.mas_top);
        make.height.mas_equalTo(12);
        make.width.mas_equalTo(12);
    }];
    self.commentLabel = [[UILabel alloc]init];
    self.commentLabel.textColor = [UIColor lightGrayColor];
    self.commentLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.commentLabel];
    [self.commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(com.mas_right).offset(SMALLSPICE);
        make.top.equalTo(_playLabel.mas_top);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(30);
    }];
    
    UIImageView *time = [[UIImageView alloc]init];
    time.image = [UIImage imageNamed:@"sound_duration@2x.png"];
    [self.contentView addSubview:time];
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_commentLabel.mas_right).offset(SPACE);
        make.top.equalTo(play.mas_top);
        make.height.mas_equalTo(12);
        make.width.mas_equalTo(12);
    }];
    self.timeLabel = [[UILabel alloc]init];
    self.timeLabel.textColor = [UIColor lightGrayColor];
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(time.mas_right).offset(SMALLSPICE);
        make.top.equalTo(_playLabel.mas_top);
        make.height.mas_equalTo(20);
        make.width.mas_equalTo(30);
    }];
    self.createTime = [[UILabel alloc]init];
    self.createTime.font = [UIFont systemFontOfSize:12];
    self.createTime.textAlignment = NSTextAlignmentRight;
    self.createTime.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:self.createTime];
    [self.createTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleLabel.mas_top);
        make.left.equalTo(_titleLabel.mas_right).offset(SPACE);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
}
-(void)tapAction{
    if (self.isCleck) {
        self.PlayOrNot.image = [UIImage imageNamed:@"cell_sound_pause_n@2x.png"];
    }else{
        self.PlayOrNot.image = [UIImage imageNamed:@"cell_sound_play_n@2x.png"];
    }
    self.isCleck = !self.isCleck;
}
-(void)setCellModel:(LXAuthorDetailModel *)cellModel{
    [self.UserImage sd_setImageWithURL:[NSURL URLWithString:cellModel.coverSmall] placeholderImage:[UIImage imageNamed:ImagePlaceHolder]];
    [self.titleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(cellModel.labelHeight);
    }];
    self.titleLabel.text = cellModel.title;
    self.authorLabel.text = [NSString stringWithFormat:@"by  %@",cellModel.nickname];
    if (cellModel.playtimes > 10000) {
        CGFloat num = cellModel.playtimes/10000;
        CGFloat num1 = cellModel.playtimes/1000;
        CGFloat num2 = num1 - num*10;
        NSString *count = [NSString stringWithFormat:@"%.f.%.f万",num,num2];
        CGSize labelSize = [AppTools labelRectWithLabelSize:CGSizeMake(100, 20) LabelText:count Font:[UIFont systemFontOfSize:12]];
        [self.playLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(labelSize.width+5);
        }];
        self.playLabel.text = count;
    }else{
    CGSize labelSize = [AppTools labelRectWithLabelSize:CGSizeMake(100, 20) LabelText:[NSString stringWithFormat:@"%ld",cellModel.playtimes] Font:[UIFont systemFontOfSize:12]];
        [self.playLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(labelSize.width+3);
        }];
        self.playLabel.text = [NSString stringWithFormat:@"%ld",cellModel.playtimes];
    }
    CGSize like = [AppTools labelRectWithLabelSize:CGSizeMake(100, 20) LabelText:[NSString stringWithFormat:@"%ld",cellModel.likes] Font:[UIFont systemFontOfSize:12]];
    [self.likeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(like.width+3);
    }];
    self.likeLabel.text = [NSString stringWithFormat:@"%ld",cellModel.likes];
    
    CGSize com = [AppTools labelRectWithLabelSize:CGSizeMake(100, 20) LabelText:[NSString stringWithFormat:@"%ld",cellModel.comments] Font:[UIFont systemFontOfSize:12]];
    [self.commentLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(com.width+3);
    }];
    self.commentLabel.text = [NSString stringWithFormat:@"%ld",cellModel.comments];
   
    NSInteger timesec = cellModel.duration/60;
    NSInteger timemin = cellModel.duration - timesec*60;
    NSString *timeSec = @"";
    NSString *timeMin = @"";
    if (timesec < 10) {
        timeSec = [NSString stringWithFormat:@"0%ld",timesec];
    }else{
        timeSec = [NSString stringWithFormat:@"%ld",timesec];
    }
    if (timemin < 10) {
        timeMin = [NSString stringWithFormat:@"0%ld",timemin];
    }else{
        timeMin = [NSString stringWithFormat:@"%ld",timemin];
    }
    NSString *time = [NSString stringWithFormat:@"%@:%@",timeSec,timeMin];
    CGSize timeLabel = [AppTools labelRectWithLabelSize:CGSizeMake(100, 20) LabelText:time Font:[UIFont systemFontOfSize:12]];
    [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(timeLabel.width+3);
    }];
    self.timeLabel.text = time;
    
    self.createTime.text = [LXTimeTool getDateWithUpdatedAt:cellModel.createdAt];
}

- (void)awakeFromNib {
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

@end
